package com.scu.qingshi.config.swagger;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * Swagger文档配置
 * 使用更清晰的ui，访问地址为：doc.html
 *         <dependency>
 *             <groupId>com.github.xiaoymin</groupId>
 *             <artifactId>knife4j-spring-ui</artifactId>
 *             <version>3.0.2</version>
 *         </dependency>
 * @author qingshi
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 配置Swagger的Docket文档实例Bean
     * @param environment
     * @return
     */
    @Bean
    public Docket docket(Environment environment){

        //设置要启动Swagger的环境
        Profiles profiles = Profiles.of("dev");
        //使用environment.acceptsProfiles来判断是否是指定的环境
        boolean run = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //enable，是否启动swagger，只在生产环境中启动
                .groupName("绯村剑心")
                .enable(run)
                .select()
                //RequestHandlerSelectors,扫描包的方式
                .apis(RequestHandlerSelectors.basePackage("com.scu.qingshi"))
                // 使用swagger3.0时无法使用，但也能直接排除所有/error的接口
//                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }

//    多个组
//    @Bean
//    public Docket yinshiDocket(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("银时");
//    }

    /**
     * 配置信息
     * @return
     */
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("绯村剑心", "", "2575217256@qq.com");

        return new ApiInfo(
                "青时Swagger文档",
                "Swagger文档",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }
}

